package com.zhang.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.server.common.Result;
import com.zhang.server.controller.request.BorrowPageRequest;
import com.zhang.server.entity.Book;
import com.zhang.server.entity.Borrow;
import com.zhang.server.entity.Retur;
import com.zhang.server.entity.User;
import com.zhang.server.ex.ServiceException;
import com.zhang.server.mapper.BookMapper;
import com.zhang.server.mapper.BorrowMapper;
import com.zhang.server.mapper.UserMapper;
import com.zhang.server.service.IBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;

@Service
public class BorrowService implements IBorrowService {
    @Autowired
    BorrowMapper borrowMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    BookMapper bookMapper;

    @Override
    public List<Borrow> list() {
        return borrowMapper.list();
    }

    @Override
    public Object page(BorrowPageRequest borrowPageRequest) {
        // 将pageNum、pageSize作为参数传进方法
        PageHelper.startPage(borrowPageRequest.getPageNum(), borrowPageRequest.getPageSize());
        List<Borrow> borrowList = borrowMapper.listByCondition(borrowPageRequest);

        for(Borrow borrow : borrowList){
            LocalDate return_date = borrow.getReturn_date();
            LocalDate now = LocalDate.now();

            if(now.plusDays(1).isEqual(return_date)){
                borrow.setNote("即将到期");
            }else if(now.isEqual(return_date)){
                borrow.setNote("已到期");
            }else if(now.isAfter(return_date)){
                borrow.setNote("已过期");
            }else{
                borrow.setNote("正常借阅中");
            }
        }

        // 将查询结果放到这里,返回，就会自动分页
        return new PageInfo<>(borrowList);
    }

    @Override
    public Object returPage(BorrowPageRequest borrowPageRequest) {
        // 将pageNum、pageSize作为参数传进方法
        PageHelper.startPage(borrowPageRequest.getPageNum(), borrowPageRequest.getPageSize());
        List<Retur> returList = borrowMapper.returListByCondition(borrowPageRequest);
        // 将查询结果放到这里,返回，就会自动分页
        return new PageInfo<>(returList);
    }

    @Override
    public Integer save(Borrow borrow) {
        // 根据借书里的用户会员号查询用户
        String user_huiyuanhao = borrow.getUser_huiyuanhao();
        User user = userMapper.getByUsername(user_huiyuanhao);
        if(Objects.isNull(user)){
            throw new ServiceException("用户不存在");
        }

        // 根据借书里的书籍标准码查询书籍
        Book book = bookMapper.getByBookNo(borrow.getBook_no());
        if(Objects.isNull(book)){
            throw new ServiceException("所借图书不存在");
        }

        // 获取到用户积分余额和这本书需要花费的积分
        Integer account = user.getAccount();
        Integer score = book.getScore() * borrow.getDays();
        if(score > account){
            throw new ServiceException("用户积分不足");
        }
        if(book.getNums() <= 0){
            throw new ServiceException("图书数量不足");
        }

        // 更新用户账户
        user.setAccount(account - score);
        userMapper.update(user);

        // 更细图书库存
        book.setNums(book.getNums() - 1);
        bookMapper.update(book);

        borrow.setReturn_date(LocalDate.now().plus(borrow.getDays(), ChronoUnit.DAYS));

        // 添加借书记录
        return borrowMapper.save(borrow);
    }

    @Override
    public Integer returSave(Retur retur) {
        Book book = bookMapper.getByBookNo(retur.getBook_no());
        book.setNums(book.getNums() + 1);
        bookMapper.update(book);
        retur.setReal_date(LocalDate.now());
        return borrowMapper.returSave(retur);
    }

    @Override
    public Result getById(Integer id) {
        return Result.success(borrowMapper.getById(id));
    }

    @Override
    public void update(Borrow borrow) {
        borrowMapper.update(borrow);
    }

    @Override
    public void delete(Integer id) {
        borrowMapper.delete(id);
    }

    @Override
    public void returDelete(Integer id) {
        borrowMapper.returDelete(id);
    }
}
